#include<iostream>
using namespace std;
long long jz[2002][2002];
long long maxa[2002][2002];
long long minm[2002][2002];
long long rmaxa[2002][2002];
long long rminm[2002][2002];
long long q[2002],q1[2002];
int main(){
    long long a,b,n;
    cin>>a>>b>>n;
    for(long long i=1;i<=a;i++)
    {
        for(long long j=1;j<=b;j++)cin>>jz[i][j];
    }
    long long head=0;
        long long tail=0;
    for(long long i=1;i<=a;i++)
    {
         head=0;
         tail=0;
        for(long long j=1;j<=b;j++)
        {
            while(head<tail&&q[head]+n<=j)head++;
            while(head<tail&&jz[i][j]>jz[i][q[tail-1]])tail--;
            q[tail]=j;
            tail++;
            if(j>=n)maxa[i][j-n+1]=jz[i][q[head]];
        }
         head=0;
         tail=0;
          for(long long j=1;j<=b;j++)
        {
            while(head<tail&&q1[head]+n<=j)head++;
            while(head<tail&&jz[i][j]<jz[i][q1[tail-1]])tail--;
            q1[tail]=j;
            tail++;
            if(j>=n)minm[i][j-n+1]=jz[i][q1[head]];
        }
    }
    for(long long i=1;i<=b-n+1;i++)
    {
        head=0;
         tail=0;
        for(long long j=1;j<=a;j++)
        {
            while(head<tail&&q[head]+n<=j)head++;
            while(head<tail&&maxa[j][i]>maxa[q[tail-1]][i])tail--;
            q[tail]=j;
            tail++;
            if(j>=n)rmaxa[j-n+1][i]=maxa[q[head]][i];
        }
        head=0;
         tail=0;
          for(long long j=1;j<=a;j++)
        {
            while(head<tail&&q1[head]+n<=j)head++;
            while(head<tail&&minm[j][i]<minm[q1[tail-1]][i])tail--;
            q1[tail]=j;
            tail++;
            if(j>=n)rminm[j-n+1][i]=minm[q1[head]][i];
        }
    }
    long long ans=10000000000000;//初始值应该尽可能大
    for(long long i=1;i<=a-n+1;i++){
         for(long long j=1;j<=b-n+1;j++){
            if(rmaxa[i][j]-rminm[i][j]<ans)ans=rmaxa[i][j]-rminm[i][j];
        }
        
    }
       cout<<ans;
    // for(long long i=1;i<=a;i++)
    //     {
    //         for(long long j=1;j<=b;j++)cout<<maxa[i][j]<<" ";
    //         cout<<endl;
    //     }
    return 0;
}